import Vue from 'vue'
import Router from 'vue-router'
//登录
import login from '../components/login.vue'
//主页
import home from '../components/home.vue'
//欢迎
import Welcom from '../components/Welcom.vue'
// 用户
import Users from '../components/User/Users.vue'
//权限
import Rights from '../components/power/Rights.vue'
//角色
import Roles from '../components/power/Roles.vue'
// 商品
import Cate from '../components/goods/Cate.vue'
import Goods from '../components/goods/Goods.vue'
import Params from '../components/goods/Params.vue'
import Adds from '../components/goods/Adds.vue'
import Report from '../components/Report/Reports.vue'
import Orders from '../components/Orders/Orders.vue'
Vue.use(Router)
const router= new Router({
  routes:[
    {
      path:'/',
      redirect:'/login'
    },
    {
      path:"/login",
      component:login
    },
    {
      path:'/home',
      component:home,
     redirect:'/welcom',
     children:[
       {
         path:'/welcom',
         component:Welcom
       },
       {
         path:'/users',
         component:Users
       },
       {
         path:'/rights',
         component:Rights,
       },
       {
         path:'/roles',
         component:Roles

       },
       {
         path:'/categories',
         component:Cate
       },
       {
        path:'/goods',
        component:Goods
      },
      {
        path:'/params',
        component:Params
      },
      {
        path:'/goods/adds',
        component:Adds
      },
      {
        path:'/reports',
        component:Report
      },
      {
        path:'/orders',
        component:Orders
      }
     ]
      
    }
  ]
})

    //位路由添加导航守卫 beforeEach
               // to 要访问的页面路径   from 从哪里跳转  next 放行函数next('/login)
               router.beforeEach((to,from,next)=>{
                if(to.path=='/login') return next()

                // 从sessionStorage中获取token
                const totkenStr=window.sessionStorage.getItem('token')
                if(!totkenStr)return next('/login')
                next()

            })

      export default  router